/**
 * 文 件 名:  ChannelController.java
 * 版      权:  财之道  Copyright ©2011-2015 All Rights Reserved
 * 描      述:  
 */
package com.czd.support.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.czd.support.model.Channel;
import com.czd.support.model.Manager;
import com.czd.support.model.Site;
import com.czd.support.model.TreeModel;
import com.czd.support.service.ChannelService;
import com.czd.support.service.SiteService;
import com.czd.support.util.ResponseUtils;

/**
 * @Description  栏目控制类
 * @Author  Jun.Yang
 * @Date    2015年1月5日 下午1:25:14
 * @Version v1.0
 */
@Controller
@RequestMapping("/channel")
public class ChannelController extends BaseController {
    @Autowired
	private ChannelService channelService;
    @Autowired
    private SiteService siteService;
    
	@RequestMapping("/list")
	public String list() {
		return "channel/list";
	}
	
	@ModelAttribute("initSiteList")
	private List<Site> initSiteList() {
		return siteService.findSiteList();
	}
	
	/**
	 * 加载栏目树
	 * @param response
	 */
	@RequestMapping(value="/treeAll", method=RequestMethod.POST)
	@ResponseBody
	public void treeAll(HttpServletResponse response) {
		List<TreeModel> channelTree = channelService.createChannelTree();
		String jsonStr = JSON.toJSONString(channelTree);
		ResponseUtils.renderJson(response,jsonStr);
		
	}
	
	/**
	 * 获取节点下的所有子节点数据
	 * @param level
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/channels/{level}", method=RequestMethod.GET)
	public String treeByParent(@PathVariable String level, Model model) {
		List<Channel> channelList = channelService.listByParent(level);
		model.addAttribute("channelList" , channelList);
		
		return "channel/list_child";
	}
	
	/**
	 * 跳转到栏目添加页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String add(HttpServletRequest request, Model model) {
		String level = request.getParameter("level");
		model.addAttribute("level", level);
		return "channel/add";
	}
	
	/**
	 * 栏目添加
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public String add(HttpServletRequest request, Channel channel,Model model) {
		Manager manager = this.getSesionUser(request);
		channel.setCreateUser(manager.getUserCode());
		channel.setUpdateUser(manager.getUserCode());
		channel.setChild(0);
		
		if(channelService.insert(channel)) {
			Channel ch = channelService.findByCode(channel.getLevel()); //查找父栏目
			if(ch.getChild() != 1) {
				ch.setChild(1);
				channelService.update(ch);	//更新父栏目
			}
			return "redirect:channels/"+channel.getLevel();
		}
		
		return error(model, "操作失败", "添加栏目信息失败");
	}
	
	/**
	 * 跳转到栏目添加页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/{id}/edit", method=RequestMethod.GET)
	public String edit(@PathVariable int id, Model model) {
		Channel channel = channelService.findById(id);
		if(channel!=null) {
			model.addAttribute("channel", channel);
			return "channel/edit";
		}
		
		return error(model, "操作失败", "栏目信息不存在");
	}
	
	
	@RequestMapping(value="/edit", method=RequestMethod.POST)
	public String edit(HttpServletRequest request, Channel channel, Model model) {
		Manager manager = this.getSesionUser(request);
		channel.setUpdateUser(manager.getUserCode());
		if(channelService.update(channel)) {
			Channel ch = channelService.findByCode(channel.getLevel()); //查找父栏目
			if(ch.getChild() != 1) {
				ch.setChild(1);
				channelService.update(ch);	//更新父栏目
			}
			return "redirect:channels/"+channel.getLevel();
		}
		
		return error(model, "操作失败", "栏目信息更新失败");
	}
	
	@RequestMapping(value="/{id}/delete", method=RequestMethod.GET)
	public String delete(HttpServletRequest request,@PathVariable int id, Model model) {
		String level = request.getParameter("level");
		if(channelService.delete(id)) {
			return "redirect:channels/"+level;
		}
		
		return error(model, "操作失败", "栏目信息删除失败");
	}
	
	/**
	 * 查看栏目详情
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/{id}/details", method=RequestMethod.GET)
	public String details(@PathVariable int id, Model model) {
		Channel channel = channelService.findById(id);
		if(channel!=null) {
			model.addAttribute("channel", channel);
			return "channel/details";
		}
		
		return error(model, "操作失败", "修改栏目信息失败");
	}
	
	/**
	 * 获取栏目信息
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/getChannelJson", method = RequestMethod.POST)
	@ResponseBody
	public void getChannelJson(HttpServletRequest request, HttpServletResponse response) {
		String code = request.getParameter("code");
		String jsonStr = null;
		if(StringUtils.isNotBlank(code)) {
			Channel channel = channelService.findByCode(code);
			jsonStr = JSON.toJSONString(channel);
		}
		
		System.out.println("ssss:"+jsonStr);
		ResponseUtils.renderText(response, jsonStr);
	}
}
